package com.ocom.common.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ocom.common.vo.GzhParamVo;

/**
 * @Description: 统一支付公众号参数格式化
 * @Author: 朱晖
 * @Date: 2023/5/23
 **/

public class GzhParamFormat {
	//type 1 微信
	public static GzhParamVo formate(JSONObject jsonObject, Integer type) {
		GzhParamVo gzhParamVo = new GzhParamVo();
		if (type == 1) {
			JSONObject jsonObject1 = JSON.parseObject(jsonObject.getString("prepay_id"));
			gzhParamVo.setOutTradeNo(jsonObject.getString("out_trade_no"));
			gzhParamVo.setTimeStamp(jsonObject1.getString("timeStamp"));
			gzhParamVo.setNonceStr(jsonObject1.getString("nonceStr"));
			gzhParamVo.setAppid(jsonObject1.getString("appId"));
			gzhParamVo.setSign(jsonObject1.getString("paySign"));
			gzhParamVo.setSignType(jsonObject1.getString("signType"));

			String prepayId = jsonObject1.getString("prepay_id");
			if (CommonUtil.isNull(jsonObject1.getString("prepay_id"))) {
				prepayId = jsonObject1.getString("package");
			}

			if (prepayId != null && prepayId.startsWith("prepay_id=")) {
				prepayId = prepayId.substring(10);
			}

			gzhParamVo.setPrepayId(prepayId);
			gzhParamVo.setType(1);

			if(CommonUtil.isNull(gzhParamVo.getSign())){
				gzhParamVo.setSign(jsonObject1.getString("sign"));
			}
			if(CommonUtil.isNull(gzhParamVo.getAppid())){
				gzhParamVo.setAppid(jsonObject1.getString("appid"));
			}
			if(CommonUtil.isNull(gzhParamVo.getNonceStr())){
				gzhParamVo.setNonceStr(jsonObject1.getString("nonce_str"));
			}
			if(!CommonUtil.isNull(jsonObject1.getString("mch_id"))){
				gzhParamVo.setMchId(jsonObject1.getString("mch_id"));
			}
			if(!CommonUtil.isNull(jsonObject1.getString("sub_mch_id"))){
				gzhParamVo.setSubMchId(jsonObject1.getString("sub_mch_id"));
			}
			if(CommonUtil.isNull(gzhParamVo.getSignType())){
				gzhParamVo.setSignType("MD5");
			}
		}
		if (type == 2) {
			gzhParamVo.setType(2);
			gzhParamVo.setCode_url(jsonObject.getString("code_url"));
		}

		if (type == 3) {

			gzhParamVo.setOutTradeNo(jsonObject.getString("out_trade_no"));
			String prepayId = "";

			gzhParamVo.setAppid(jsonObject.getString("payAppId"));

			if (!CommonUtil.isNull(jsonObject.getString("paySignType"))) {
				gzhParamVo.setSignType(jsonObject.getString("paySignType"));
			}

			if (!CommonUtil.isNull(jsonObject.getString("paynonceStr"))) {
				gzhParamVo.setNonceStr(jsonObject.getString("paynonceStr"));
			}

			if (!CommonUtil.isNull(jsonObject.getString("paySign"))) {
				gzhParamVo.setSign(jsonObject.getString("paySign"));
			}

			if (!CommonUtil.isNull(jsonObject.getString("payPackage"))) {
				prepayId = jsonObject.getString("payPackage");
			}
			if (prepayId != null && prepayId.startsWith("prepay_id=")) {
				prepayId = prepayId.substring(10);
			}
			gzhParamVo.setPrepayId(prepayId);

			if (!CommonUtil.isNull(jsonObject.getString("ordNo"))) {
				gzhParamVo.setOutTradeNo(jsonObject.getString("ordNo"));
			}
			if (!CommonUtil.isNull(jsonObject.getString("payTimeStamp"))) {
				gzhParamVo.setTimeStamp(jsonObject.getString("payTimeStamp"));
			}
			gzhParamVo.setType(1);
		}
		return gzhParamVo;
	}
}
